﻿using System;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
using System.ServiceModel.Description;
using VideoStore.Services;
using VideoStore.Services.Interface;
using Warehouse.Services;
using Warehouse.Services.Interface;

namespace TestDriver.Warehouse.Host
{
    class Program
    {
        private const string MSG = "{0} Service started!";

        static void Main(string[] args)
        {
            CreateQueue();

            Console.WriteLine("Starting Warehouse services...");
            Console.WriteLine();
            try
            {
                var deliveryHost = new ServiceHost(typeof(DeliveryService));
                var smbDelivery = new ServiceMetadataBehavior();
                deliveryHost.Description.Behaviors.Add(smbDelivery);
                deliveryHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://localhost:8001/DeliveryService/mex/");
                var deliveryBinding = new NetTcpBinding();
                deliveryBinding.TransactionFlow = true;
                deliveryBinding.TransactionProtocol = TransactionProtocol.OleTransactions;
                deliveryHost.AddServiceEndpoint(typeof(IDeliveryService), deliveryBinding, "net.tcp://localhost:8001/DeliveryService");
                deliveryHost.Open();
                Console.WriteLine(MSG, "Delivery");

                var deliveryQueuedHost = new ServiceHost(typeof(DeliveryServiceQueued));
                var smbDeliveryQueued = new ServiceMetadataBehavior();
                deliveryQueuedHost.Description.Behaviors.Add(smbDeliveryQueued);
                deliveryQueuedHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://localhost:8001/DeliveryServiceQueued/mex/");
                var deliveryQueuedBinding = new NetMsmqBinding();
                deliveryQueuedBinding.Security.Mode = NetMsmqSecurityMode.None;
                deliveryQueuedBinding.Durable = true;
                deliveryQueuedBinding.ExactlyOnce = true;
                deliveryQueuedHost.AddServiceEndpoint(typeof(IDeliveryServiceQueued), deliveryQueuedBinding, "net.msmq://localhost/private/VideoStore/ServiceQueue");
                deliveryQueuedHost.Open();
                Console.WriteLine(MSG, "Delivery (Queued)");

                Console.WriteLine();
                Console.WriteLine("All Services started successfully!");
                Console.WriteLine();
                Console.WriteLine("Now listening to any Services update ... ");
                Console.WriteLine();
                Console.ReadKey();
            }
            catch (CommunicationException cmEx)
            {
                Console.WriteLine("Could not start services. Error: " + cmEx.Message);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Could not start services. Error: " + ex.Message);
                Console.ReadKey();
            }
        }

        private static void CreateQueue()
        {
            string queueName = ConfigurationManager.AppSettings["queueName"];
            if (!MessageQueue.Exists(queueName))
            {
                MessageQueue.Create(queueName, true);
            }

            queueName = ConfigurationManager.AppSettings["statusQueueName"];
            if (!MessageQueue.Exists(queueName))
            {
                MessageQueue.Create(queueName, true);
            }
        }
    }
}
